/**
 * 目标。 删除console.log
 */

import generate from '@babel/generator'
import { parse } from '@babel/parser'
import traverse from '@babel/traverse'

import fs from 'node:fs'
import path from 'node:path'

const target = path.resolve(__dirname, '../demo/04.js')

const content = fs.readFileSync(target, {
	encoding: 'utf-8',
})

let ast = parse(content, {
	sourceType: 'module',
})

traverse(ast, {
	CallExpression(path) {
		const calleeStr = generate(path.node.callee)

		if (calleeStr.code.indexOf('console.') !== -1) {
      // 删除console这个节点路径
			path.remove()
		}
	},
})

const { code } = generate(ast, {
  jsescOption: {
    minimal: true
  }
})

let dir = path.resolve(__dirname, '../demo/04-1.js')
fs.writeFileSync(dir, code)
